package com.h5lib.xlsx4j;

import org.xmlpull.v1.XmlPullParser;
import org.xmlpull.v1.XmlPullParserException;

import java.io.IOException;
import java.util.*;

public class XMLSheetReader implements Iterable<XLSXSheetRow> {

    private XmlPullParser inputStream;
    private XLSXSheetRowIterator rowsIterator = null;
    private List<String> sharedStrings;

    private Stack<XMLTag> stack = new Stack<XMLTag>();

    public XMLSheetReader(XmlPullParser newPullParser) {
        inputStream = newPullParser;
        sharedStrings = new ArrayList<String>();
    }

    public void setSharedStrings(List<String> collection) {
        sharedStrings = collection;
    }

    private boolean inSheetData = false;

    /**
     *
     * fixme
     *
     * @throws XMLStreamException
     * @throws IOException 
     * @throws XmlPullParserException 
     */
    private void moveToSheetData() throws XmlPullParserException, IOException {

    	int event = inputStream.getEventType();
    	
        while ((event) != XmlPullParser.END_DOCUMENT) {
            if (event == XmlPullParser.START_TAG) {

                stack.push(new XMLTag(inputStream.getName()));
                if ("sheetData".equals(inputStream.getName())) {
                    break;
                }
            }

            if (event == XmlPullParser.END_TAG) {
                stack.pop();
            }
            event = inputStream.next();
        }
    }

    public XLSXSheetRow next() throws NumberFormatException, XmlPullParserException, IOException  {

        if (inSheetData) {
            moveToSheetData();
            inSheetData = true;
        }
        String tagContent = null;
        XLSXSheetRow sheetRow = new XLSXSheetRow();

        int parsedRows = 0;
        int event = inputStream.getEventType();
        
        while ((event) != XmlPullParser.END_DOCUMENT && parsedRows < 1) {

            switch (event) {
                case XmlPullParser.START_TAG:
				String name = inputStream.getName();
				stack.push(new XMLTag(name));
                    if (stack.peek().equals("c")) {
                        String attributeValue = inputStream.getAttributeValue(null, "t");
                        if(attributeValue == null){
                        	attributeValue = "";
                        }
						stack.peek().getProperties().setProperty("t", attributeValue);
                    }
                    break;

                case XmlPullParser.END_TAG:

                    XMLTag tag = stack.pop();

                    if (tag.equals("v")) {
                        String text = tagContent;

                        if (stack.peek().getProperties().getProperty("t").equals("s")) {
                            text = sharedStrings.get(new Integer(tagContent));
                        }
                        sheetRow.add(text);
                    }

                    if (tag.equals("row")) {
                        parsedRows++;
                    }

                    tagContent = null;

                    break;

                case XmlPullParser.TEXT:
                    tagContent = inputStream.getText().trim();
                    break;
            }
            event = inputStream.next();
        }

        return parsedRows > 0 ? sheetRow : null;
    }

    @Override
    public XLSXSheetRowIterator iterator() {
        if( rowsIterator == null ) {
            rowsIterator = new XLSXSheetRowIterator(this);
        }

        return rowsIterator;
    }
}
